package com.changeover.gwt.client.view.impl;

import com.changeover.gwt.client.view.ILoginView;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginView extends Composite implements ILoginView {
    private final Button loginButton;
    private final TextBox loginField;
    private final PasswordTextBox passwordField;
    private final Label errorLabel;

    public LoginView() {
        loginButton = new Button("Login Button");
        loginButton.setText("Login");

        DecoratorPanel contentDecorator = new DecoratorPanel();

        HorizontalPanel labelPanel = new HorizontalPanel();
        labelPanel.setSpacing(5);

        Label lblLogin = new Label("Login:");
        labelPanel.add(lblLogin);
        lblLogin.setWidth("56px");

        HorizontalPanel buttonsPanel = new HorizontalPanel();
        buttonsPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        buttonsPanel.setSpacing(5);

        errorLabel = new Label("");
        errorLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        buttonsPanel.add(errorLabel);
        errorLabel.setWidth("193px");
        buttonsPanel.add(loginButton);
        loginButton.setWidth("69px");

        VerticalPanel panel = new VerticalPanel();
        panel.setSpacing(10);
        panel.add(labelPanel);
        labelPanel.setWidth("277px");

        loginField = new TextBox();
        labelPanel.add(loginField);
        loginField.setWidth("194px");

        HorizontalPanel horizontalPanel = new HorizontalPanel();
        horizontalPanel.setSpacing(5);
        panel.add(horizontalPanel);
        horizontalPanel.setWidth("277px");

        Label lblPassword = new Label("Password:");
        horizontalPanel.add(lblPassword);
        lblPassword.setWidth("56px");

        passwordField = new PasswordTextBox();
        horizontalPanel.add(passwordField);
        passwordField.setWidth("189px");
        panel.add(buttonsPanel);
        buttonsPanel.setWidth("277px");

        contentDecorator.add(panel);

        initWidget(contentDecorator);

    }

    @Override
    public HasClickHandlers getLoginButton() {
        return loginButton;
    }

    @Override
    public String getLoginText() {
        return loginField.getText();
    }

    @Override
    public String getPasswordText() {
        return passwordField.getText();
    }

    @Override
    public void setErrorText(String errorMessage) {
        errorLabel.setText(errorMessage);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void clean() {
        loginField.setText("");
        passwordField.setText("");
    }

    @Override
    public Button getLoginButton2() {
        return loginButton;
    }

    @Override
    public Label getErrorLabel() {
        return errorLabel;
    }

}
